package com.gitee.freakchicken.dbapi.basic.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gitee.freakchicken.dbapi.basic.domain.DataSource;
import com.gitee.freakchicken.dbapi.basic.domain.JdbcDataSource;
import com.gitee.freakchicken.dbapi.basic.service.C_qHUtLNkz;
import com.gitee.freakchicken.dbapi.basic.util.C_rezNpsos;
import com.gitee.freakchicken.dbapi.basic.util.C_LS6YRMwy;
import com.gitee.freakchicken.dbapi.basic.util.C_3CjyYmwF;
import com.gitee.freakchicken.dbapi.common.ResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/datasource")
public class C_VGQ6zX1F {

    @Autowired
    C_qHUtLNkz gf_mCeJPcTz;

    @RequestMapping("/add")
    public ResponseDto m_Cr8cwi7x(@RequestBody JSONObject jo) {
        DataSource lf_eUM78PKc = new DataSource();
        lf_eUM78PKc.setName(jo.getString("name"));
        lf_eUM78PKc.setNote(jo.getString("note"));
        lf_eUM78PKc.setType(jo.getString("type"));
        JSONObject lf_d1lu8Mv4 = jo.getJSONObject("detail");
        if (lf_eUM78PKc.isJdbcType()) {
            JdbcDataSource lf_D1sCQymp = lf_d1lu8Mv4.toJavaObject(JdbcDataSource.class);
            try {
                lf_D1sCQymp.setPassword(C_rezNpsos.m_GtoIPd34(lf_D1sCQymp.getPassword()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            lf_eUM78PKc.setDetail(JSON.toJSONString(lf_D1sCQymp));
        }
        lf_eUM78PKc.setCreateUserId(C_3CjyYmwF.m_j3EEUXak());
        return gf_mCeJPcTz.m_uSBehoQg(lf_eUM78PKc);
    }

    @RequestMapping("/getAll")
    public List<DataSource> m_KEEkkhJB() {
        return gf_mCeJPcTz.m_fKTVDaPR();
    }

    @RequestMapping("/getAllByType")
    public List<DataSource> m_4p3DH8Gn(String type) {
        return gf_mCeJPcTz.m_UCO71yVI(type);
    }

    @RequestMapping("/detail/{id}")
    public DataSource m_jhqpSzdA(@PathVariable String id) {
        return gf_mCeJPcTz.m_NQrcJ8gx(id);
    }

    @RequestMapping("/delete/{id}")
    public ResponseDto m_uUhgdYjY(@PathVariable String id) {
        return gf_mCeJPcTz.m_btQpfuV2(id);
    }

    @RequestMapping("/update")
    public void m_As36As1T(@RequestBody JSONObject jo) {
        DataSource lf_WImydDgp = new DataSource();
        lf_WImydDgp.setId(jo.getString("id"));
        lf_WImydDgp.setName(jo.getString("name"));
        lf_WImydDgp.setNote(jo.getString("note"));
        lf_WImydDgp.setType(jo.getString("type"));
        JSONObject lf_cj4NmDrD = jo.getJSONObject("detail");
        if (lf_WImydDgp.isJdbcType()) {
            JdbcDataSource lf_DkWLnKdC = lf_cj4NmDrD.toJavaObject(JdbcDataSource.class);
            if (lf_cj4NmDrD.getBoolean("edit_password")) {
                try {
                    lf_DkWLnKdC.setPassword(C_rezNpsos.m_GtoIPd34(lf_DkWLnKdC.getPassword()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            lf_WImydDgp.setDetail(JSON.toJSONString(lf_DkWLnKdC));
        }
        lf_WImydDgp.setCreateUserId(C_3CjyYmwF.m_j3EEUXak());
        gf_mCeJPcTz.m_Ehj42ygv(lf_WImydDgp);
    }

    @RequestMapping("/connect")
    public ResponseDto m_pYVMv0A3(JdbcDataSource dataSource) {
        Connection lf_6zvNbElT = null;
        try {
            lf_6zvNbElT = C_LS6YRMwy.m_pME6ivnU(dataSource);
            return ResponseDto.apiSuccess(null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseDto.fail(e.getMessage());
        } finally {
            if (lf_6zvNbElT != null) {
                try {
                    lf_6zvNbElT.close();
                } catch (SQLException e) {
                    log.error(e.getMessage());
                }
            }
        }
    }

    @RequestMapping("/export")
    public void m_Zm0N1I4N(String ids, HttpServletResponse response) {
        List<String> lf_faeY6PlU = Arrays.asList(ids.split(","));
        List<DataSource> lf_Juftsxci = gf_mCeJPcTz.m_pL3R8HyB(lf_faeY6PlU);
        String lf_PYWfKEz8 = JSON.toJSONString(lf_Juftsxci);
        response.setContentType("application/x-msdownload;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=datasource.json");
        OutputStream lf_tVql5wbG = null;
        try {
            lf_tVql5wbG = response.getOutputStream();
            lf_tVql5wbG.write(lf_PYWfKEz8.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (lf_tVql5wbG != null)
                    lf_tVql5wbG.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/import", produces = "application/json;charset=UTF-8")
    public void m_lrEjCGol(@RequestParam("file") MultipartFile file) throws IOException {
        String lf_ARrIfNwx = IOUtils.toString(file.getInputStream(), "utf-8");
        List<DataSource> lf_aqune7JI = JSON.parseArray(lf_ARrIfNwx, DataSource.class);
        lf_aqune7JI.stream().forEach(lambda_lf_7DJ0f54S -> {
            lambda_lf_7DJ0f54S.setCreateUserId(C_3CjyYmwF.m_j3EEUXak());
            lambda_lf_7DJ0f54S.setCreateTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
            lambda_lf_7DJ0f54S.setUpdateTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
        });
        gf_mCeJPcTz.m_zKXpVS6p(lf_aqune7JI);
    }
}
